import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:goumao_app/Controller/gm_showweb_page.dart';

class ListItem {
  String text;
  GestureTapCallback onTap;

  ListItem({required this.text, required this.onTap});
}

class GMAppUtil {
  static const Color kAppThemColor = Color(0xff73ed94); // 定义主题颜色
  static const Color kWhiteColor = Color(0xffffffff); // 定义主题颜色

  static double kScreenWidth = 0.0;
  static double kScreenHeight = 0.0;

  // 初始化屏幕宽度和高度
  static void init(BuildContext context) {
    kScreenWidth = MediaQuery.of(context).size.width;
    kScreenHeight = MediaQuery.of(context).size.height;
  }

  // 其他常量
  static const double borderRadius = 8.0;
  // static const Color primaryColor = Colors.orange;

  static const TextStyle kNavTextStyle = TextStyle(
      fontSize: 16, color: Color(0xff333333), fontWeight: FontWeight.w900);

  static gmToast(String title) {
    Fluttertoast.showToast(
        msg: title,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black,
        textColor: const Color(0xffffffff),
        fontSize: 16.0);
  }

  static gmShowTermServece(BuildContext context, int index) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => GMShowWebPagge(
                  title: index == 1 ? "隐私协议" : "用户协议",
                  url: index == 1
                      ? "http://mtw.so/6gTpZ8"
                      : "http://mtw.so/6gTpWS",
                )));
  }

  static navigateToLogin(BuildContext context) {
    // if (!UserManager().isLoggedIn) {
    //   Navigator.push(
    //       context,
    //       CupertinoPageRoute(
    //           builder: (context) {
    //             return const LoginPage();
    //           },
    //           fullscreenDialog: true));
    // }
  }

  static userReport(BuildContext context) {
    ontap() => {Navigator.of(context).pop(), GMAppUtil.gmToast("我们会尽快处理你的举报")};
    List<ListItem> myList = [
      ListItem(text: "资料造假", onTap: ontap),
      ListItem(text: "恶意骚扰", onTap: ontap),
      ListItem(text: "垃圾广告", onTap: ontap),
      ListItem(text: "色情低俗", onTap: ontap),
      ListItem(text: "诱导欺骗", onTap: ontap)
    ];
    popFromBottomView(context, "请选择举报类型", myList);
  }

  static popFromBottomView(
      BuildContext context, String title, List<ListItem> myList) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SafeArea(
          child: SizedBox(
            height: myList.length * 48 + (title.isNotEmpty ? 64 : 14),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Visibility(
                  visible: title.isNotEmpty, // 根据条件设置可见性
                  child: SizedBox(
                    height: 50,
                    child: Center(
                      child: Text(
                        title,
                        style: const TextStyle(
                            color: Color(0xff1a1a1a),
                            fontSize: 16,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                  ),
                ),
                Expanded(
                    child: ListView.builder(
                        itemCount: myList.length, // 列表总数
                        itemBuilder: (context, index) => ListTile(
                              title: Center(
                                child: Text(myList[index].text),
                              ),
                              onTap: myList[index].onTap,
                            )))
              ],
            ),
          ),
        );
      },
    );
  }

  static Future<Map<String, dynamic>> toLoadData(String path) async {
    String jsonString = await rootBundle.loadString(path);
    Map<String, dynamic> jsonData = json.decode(jsonString);

    return jsonData;
  }

  static Widget navText(String title) {
    return Text(
      title,
      style: kNavTextStyle,
    );
  }

  static const Color viewBackgroudColor = Color(0xFF19191E);
  static const Color subViewBackgroudColor = Color(0xFF28282F);
  static const Color subViewBackgroudColor2 = Color(0xFF3B3C42);
  static const Color kThemColor = Color(0xFFFF6A81);
  static const Color kNormalColor = Color(0xFFCCCCCC);

  static const int _primaryColorValue = 0xFFFFFFFF;
  static const Color primaryColor = Color(_primaryColorValue);
  static const MaterialColor primarySwatchColor = MaterialColor(
    _primaryColorValue,
    <int, Color>{
      50: Color(0xFFFFFFFF),
      100: Color(0xFFFFFFFF),
      200: Color(0xFFFFFFFF),
      300: Color(0xFFFFFFFF),
      400: Color(0xFFFFFFFF),
      500: Color(0xFFFFFFFF),
      600: Color(0xFFFFFFFF),
      700: Color(0xFFFFFFFF),
      800: Color(0xFFFFFFFF),
      900: Color(0xFFFFFFFF),
    },
  );
}
